﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;

namespace V5_DBUtility {
    public class DBCommonHelper {
        #region
        public static T ConvertToModel<T>(DataRow dr) where T : new() {
            T t = new T();
            Type modelType = t.GetType();
            foreach (PropertyInfo pi in modelType.GetProperties()) {
                if (pi == null) continue;
                if (pi.CanWrite == false) continue;

                if (dr.Table.Columns.Contains(pi.Name)) {
                    //p.SetValue(t, GetDefaultValue(dr[p.Name], p.PropertyType), null);
                    try {
                        if (dr[pi.Name] != DBNull.Value)
                            pi.SetValue(t, dr[pi.Name], null);
                        else
                            pi.SetValue(t, default(object), null);
                    }
                    catch {
                        pi.SetValue(t, GetDefaultValue(dr[pi.Name], pi.PropertyType), null);
                    }
                }

            }
            return t;
        }
        private static object GetDefaultValue(object obj, Type type) {
            if (obj == DBNull.Value) {
                return default(object);
            }
            else {
                return Convert.ChangeType(obj, type);
            }
        }


        /// <summary>
        /// dataset转实体类 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="ds"></param>
        /// <returns></returns>
        public static List<T> FillModel<T>(DataSet ds) {
            List<T> l = new List<T>();
            T model = default(T);
            if (ds.Tables[0].Columns[0].ColumnName == "rowId") {
                ds.Tables[0].Columns.Remove("rowId");
            }

            foreach (DataRow dr in ds.Tables[0].Rows) {

                model = Activator.CreateInstance<T>();
                foreach (DataColumn dc in dr.Table.Columns) {
                    PropertyInfo pi = model.GetType().GetProperty(dc.ColumnName);
                    if (dr[dc.ColumnName] != DBNull.Value)
                        pi.SetValue(model, dr[dc.ColumnName], null);
                    else
                        pi.SetValue(model, null, null);
                }
                l.Add(model);
            }
            return l;

        }
        /// <summary> 
        /// 将实体类转换成DataTable 
        /// </summary> 
        /// <typeparam name="T"></typeparam> 
        /// <param name="i_objlist"></param> 
        /// <returns></returns> 
        public static DataTable Fill<T>(IList<T> objlist) {
            if (objlist == null || objlist.Count <= 0) {
                return null;
            }
            DataTable dt = new DataTable(typeof(T).Name);
            DataColumn column;
            DataRow row;
            System.Reflection.PropertyInfo[] myPropertyInfo = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (T t in objlist) {
                if (t == null) {
                    continue;
                }
                row = dt.NewRow();
                for (int i = 0, j = myPropertyInfo.Length; i < j; i++) {
                    System.Reflection.PropertyInfo pi = myPropertyInfo[i];
                    string name = pi.Name;
                    if (dt.Columns[name] == null) {
                        column = new DataColumn(name, pi.PropertyType);
                        dt.Columns.Add(column);
                    }
                    row[name] = pi.GetValue(t, null);
                }
                dt.Rows.Add(row);
            }
            return dt;
        }
        #endregion
    }
}
